import { Node, Widget } from 'cc';
import PokerItem from "../data/PokerItem";
import PokerArray from "../data/PokerArray";
import PokerCompose from "../data/PokerCompose";
import { TextureConfig } from "../config/TextureConfig";
import { GameManager, ScoreCall } from "./GameManager";
import { Intensify, Settle } from "../data/PokerIntensify";
import { PokerConfig } from '../config/PokerConfig';
import { size } from 'cc';

export abstract class MagicPoker {

    constructor(public readonly desc: string, public readonly name: string,) {

    }

    isBuffer() {
        return this instanceof MagicBuffer.MagicBufferPoker;
    }

    isHandle() {
        return this instanceof MagicHandle.MagicHandlePoker;
    }

    isGrow() {
        return this instanceof MagicGrow.MagicGrowPoker;
    }

    getTexture() {
        return TextureConfig.Poker_Back;
    }

    getPay() {
        return 2;
    }

    getTag() {
        if (this.isBuffer()) {
            return '增益卡\n'
        }
        if (this.isHandle()) {
            return '道具卡\n'
        }
        if (this.isGrow()) {
            return '成长卡\n'
        }
        return ''
    }
}

export namespace MagicBuffer {
    /**
     * Buffer卡牌
     * 案例 .
     * 1 持有该Poker,每次换牌花费-1/+1 :: [开始时执行,执行一次] :: 操作对象 userConfig
     * 2 持有该Poker,每次上牌会,改变上牌的花色
     * 3 最后算分的时候,该牌基础分为0
     * 4 弃牌的时候,会将牌重新放在牌库
     * 5 初次上牌,会挑选其中的几张进行加强
     */
    export abstract class MagicBufferPoker extends MagicPoker {
        constructor(desc: string, name: string = '增益卡') {
            super(desc, name)
        }

        abstract onActive(game: GameManager): Function;

        initPoker(game: GameManager) {

        }

        recyclePoker(game: GameManager) {

        }

        replacePoker(game: GameManager) {

        }

        playPoker(game: GameManager, array: PokerArray) {

        }

        magicPoker(game: GameManager) {

        }

        finishPoker(game: GameManager) {

        }

        statisticSinglePoker(game: GameManager, pokerObj: PokerItem, call?: ScoreCall) {

        }

        statisticArrayPoker(game: GameManager, pokerArray: PokerArray, call?: ScoreCall) {

        }

        statisticComposePoker(game: GameManager, pokerCompose: PokerCompose, call?: ScoreCall) {

        }

        settlementPoker(game: GameManager, skipLevel: boolean) {

        }
    }

    // Buffer -----------------------------------------------------------------------------------------
    export const Poker_Init_Replace_GreaterThan_10 = new class extends MagicBufferPoker {
        constructor() {
            super('进行对大于10的牌,随机变换花色/数字\n[触发:初始上牌]', '10号换牌员')
        }

        async initPoker(game: GameManager) {

        }

        onActive(game: GameManager) {
            return game.listenerInit(this.initPoker.bind(this));
        }
    }

    export const GoodReplacePaySub = new class extends MagicBufferPoker {
        constructor() {
            super('持有该Poker,每次换牌花费-1')
        }
        onActive(game: GameManager) {
            return game.listenerInit(this.initPoker.bind(this));
        }
    }

    export const BadReplacePayAdd = new class extends MagicBufferPoker {
        constructor() {
            super('持有该Poker,每次换牌花费+1')
        }
        onActive(game: GameManager) {
            return game.listenerInit(this.initPoker.bind(this));
        }
    }

    export const MagicBufferCollect = [
        Poker_Init_Replace_GreaterThan_10,
    ];

    // 基础记分
    export const Magic_Base_Buffer = new class extends MagicBufferPoker {
        constructor() {
            super('基础计分', '基础卡');
        }
        onActive(game: GameManager) {
            const unbind = [
                game.listenerStatisticSingle(this.statisticSinglePoker.bind(this)),
                game.listenerStatisticArray(this.statisticArrayPoker.bind(this)),
                game.listenerStatisticCompose(this.statisticComposePoker.bind(this)),
                game.listenerSettlement(this.settlementPoker.bind(this)),
                game.listenerPlay(this.playPoker.bind(this))
            ]
            return () => {
                unbind.forEach(v => v())
            }
        }

        async statisticSinglePoker(game: GameManager, pokerItem: PokerItem, call?: ScoreCall) {
            const pokerValue = PokerConfig.pokerNumber(pokerItem.getPoker());

            const current_value = game.watchCurrentValue.value;
            const current_multiple = game.watchCurrentMultiple.value;

            const [v, m] = pokerItem.getIntensify().reduce((pre: [number, number], value) => {
                return value.onSettle(pre[0], pre[1], pokerItem.getPoker());
            }, [current_value + pokerValue, current_multiple]);

            const diff_V = v - current_value;
            const diff_M = m - current_multiple;

            if (call) {
                call(diff_V, diff_M);
            } else {
                game.watchCurrentValue.add(diff_V);
                game.watchCurrentMultiple.add(diff_M);
            }

            console.log('statisticSinglePoker', pokerItem.getPoker(),
                `\n before [${current_value}:${current_multiple}],
                  diff [${diff_V}:${diff_M}], 
                  after:[${game.watchCurrentValue.value}:${game.watchCurrentMultiple.value}]`)
        }

        async statisticArrayPoker(game: GameManager, pokerArray: PokerArray, call?: ScoreCall) {
            console.log('statisticArrayPoker', pokerArray.pokers.map(v => PokerConfig.pokerIcon(v.getPoker())),)
            /**
             * 进行Poker Array的结算
             * 进行组合记分
             * 链子 无分
             * 同色 +20
             * 同花 30
             * 杠子 +100 或者加比例    
             */

            //判断长链子/长对子
            const current_value = game.watchCurrentValue.value;
            const current_multiple = game.watchCurrentMultiple.value;
            if (pokerArray.length() > 3) {
                const value = 100;
                const multiple = 0;
                if (call) {
                    call(value, multiple);
                } else {
                    // 展示动画,并且动态的给分值添加一个滚动的效果
                   
                    game.watchCurrentValue.add(value);
                    game.watchCurrentMultiple.add(multiple);
                }
            }

            //判断同色
            if (pokerArray.pokerColor.size === 1) {
                const value = 150;
                const multiple = 1;
                if (call) {
                    call(value, multiple);
                } else {
                    game.watchCurrentValue.add(value);
                    game.watchCurrentMultiple.add(multiple);
                }
            }

            //判断同花
            if (pokerArray.pokerType.size === 1) {
                const value = 200;
                const multiple = 2;
                if (call) {
                    call(value, multiple);
                } else {
                    game.watchCurrentValue.add(value);
                    game.watchCurrentMultiple.add(multiple);
                }
            }
        }

        async statisticComposePoker(game: GameManager, pokerCompose: PokerCompose, call?: ScoreCall) {
            console.log('statisticArrayPoker', pokerCompose.arraies.map(a => a.pokers.map(v => PokerConfig.pokerIcon(v.getPoker()))))

        }

        async settlementPoker(game: GameManager, skipLevel: boolean) {

        }

        async playPoker(game: GameManager, array: PokerArray) {
            await game.pokerNextItem();
            if (array.length() > 3) {
                game.pokerNextItem();
            }
        }

    }
}

/**
 * 道具卡片
 * 在比赛中，使用时，需要检查，需要几张牌
 */
export namespace MagicHandle {

    export abstract class MagicHandlePoker extends MagicPoker {
        constructor(desc: string, name: string = '道具卡') {
            super(desc, name)
        }

        abstract onActive(game: GameManager): void;
    }

    export const Add_Money_10 = new class extends MagicHandlePoker {

        constructor() {
            super('使用后玩家增长10个金币\n[触发:手动触发]', '宝藏卡')
        }

        onActive(game: GameManager): void {

        }
    }

    export const MagicHandleCollect = [
        Add_Money_10,
    ];
}

export namespace MagicGrow {

    /**
     * 成长卡片
     */
    export abstract class MagicGrowPoker extends MagicPoker {
        constructor(desc: string, name: string = '成长卡') {
            super(desc, name)
        }

        abstract onActive(): Intensify
    }

    export const base_value_add_200 = new class extends MagicGrowPoker {
        constructor() {
            super('基础分+200')
        }

        onActive(): Intensify {
            return new class extends Intensify {
                constructor() {
                    super('基础分+200', { value: 200, settle: Settle.add })
                }

                onDraw(poker: Node, widgetComp: Widget): void {

                }
            }
        }
    }

    export const MagicGrowCollect = [
    ];
}
